const Ajv = require('ajv');
const ajv = new Ajv();
const qt = require('../schema/question');

const choiceQuestionVaildata = ajv.compile(qt.ChoiceQuestion)
const blankQuestionVaildata = ajv.compile(qt.blankQuestion)
const judgingQuestionVaildata = ajv.compile(qt.judgingQuestion)
const shortAnswerQuestionVaildata = ajv.compile(qt.shortAnswerQuestion)

//判断一个对象是否是系统能认可的题目格式
function isquestion(obj){
    //console.log(obj)
    try {
        return choiceQuestionVaildata(obj) ||
            blankQuestionVaildata(obj) ||
            judgingQuestionVaildata(obj) ||
            shortAnswerQuestionVaildata(obj);
    }catch(e){
        return {
            msg:"ajv发生未知错误"
        }
    }
}

//获得数组对象内被系统认可的题目
function getQuestionArray(arr){
    let result = [];
    if(Array.isArray(arr))arr.forEach(r =>{
        if(isquestion(r)) result.push(r);
    })
    return result;
}
//获得数组对象内不被系统认可的题目
function getNotQuestionArray(arr){
    let result = [];
    if(Array.isArray(arr))arr.forEach(r =>{
        if(!isquestion(r)) result.push(r);
    })
    return result;
}

// function test(){
//     let a = [
//             {
//                 "title": "度人过红文律中",
//                 "type": 5,
//                 "analysis": "dolore voluptate culpa et enim",
//                 "qt_body": {
//                     "answers": "do Ut ut in",
//                     "scoreDefault": 56
//                 },
//                 "difficulty": 61
//             },
//             {
//                 "title": "备己回增专",
//                 "type": 22,
//                 "analysis": "est",
//                 "qt_body": {
//                     "selections": [
//                         "aute dolore",
//                         "occaecat officia cillum"
//                     ],
//                     "answers": [
//                         "in laboris dolor"
//                     ],
//                     "scoreDefault": [
//                         "pariatur sunt",
//                         "commodo ea",
//                         "commodo eu non"
//                     ]
//                 },
//                 "difficulty": 44
//             }
//         ]
//
//     console.log(getNotQuestionArray(a));;
// }
//
// test();


module.exports = {
    isquestion,
    getQuestionArray,
    getNotQuestionArray
}